﻿using System.Collections.Generic;
using System.Web.Mvc;
using ShopB2C.Entity;

namespace ShopB2C.Helper {
    public static class ClassifyHelper {

        /// <summary>
        /// 创建分类的 DropDownList 或 ListBox
        /// </summary>
        /// <param name="classifyList">分类集合</param>
        /// <param name="selectName">元素名称，ID 和 Name 相同</param>
        /// <param name="selectedValue">选中的项</param>
        /// <param name="size">显示多少项</param>
        /// <param name="isAddDefault">是否加入默认选择项</param>
        /// <returns>string</returns>
        public static string ClassifyTree(
            List<ClassifyEntity> classifyList,
            string selectName,
            string selectedValue,
            int size,
            bool isAddDefault) {

            string result = string.Empty;

            result += string.Format("<select id=\"{0}\" name=\"{1}\"{2}>",
                selectName.Replace(".", "_"),
                selectName,
                size > 1 ? string.Format(" size=\"{0}\"", size) : string.Empty);
            result += new ClassifyTree(classifyList, selectedValue).CreateSelect(isAddDefault);
            result += "</select>";

            return result;
        }
        /// <summary>
        /// 获得分类
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="classifyList">分类集合</param>
        /// <returns>SelectList</returns>
        public static SelectList GetClassifyList(List<ClassifyEntity> classifyList) {
            return ClassifyHelper.GetClassifyList(classifyList, string.Empty, true);
        }
        /// <summary>
        /// 获得分类
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="classifyList">分类集合</param>
        /// <param name="selectedValue">选中的项</param>
        /// <returns>SelectList</returns>
        public static SelectList GetClassifyList(List<ClassifyEntity> classifyList, string selectedValue) {
            return ClassifyHelper.GetClassifyList(classifyList, selectedValue, true);
        }
        /// <summary>
        /// 获得分类
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="classifyList">分类集合</param>
        /// <param name="isAddDefault">是否添加默认的项</param>
        /// <returns>SelectList</returns>
        public static SelectList GetClassifyList(List<ClassifyEntity> classifyList, bool isAddDefault) {
            return ClassifyHelper.GetClassifyList(classifyList, string.Empty, isAddDefault);
        }
        /// <summary>
        /// 获得分类
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="classifyList">分类集合</param>
        /// <param name="selectedValue">选中的项</param>
        /// <param name="isAddDefault">是否添加默认的项</param>
        /// <returns>SelectList</returns>
        public static SelectList GetClassifyList(
            List<ClassifyEntity> classifyList, string selectedValue, bool isAddDefault) {
            SelectList sl = new ClassifyTree(classifyList,
                selectedValue).CreateSelectList(isAddDefault);
            return sl;
        }

    }
}
